<template>
  <div class="pb50">
    <el-form ref="form" size="small" :model="form" label-width="200px">
      <div class="common-form">充值设置</div>

      <el-form-item label="是否开启余额充值">
        <el-radio-group v-model="form.is_open">
          <el-radio label="1">开启</el-radio>
          <el-radio label="0">关闭</el-radio>
        </el-radio-group>
        <div class="lh18 mt10 gray9">
          <p>注：如开启则移动端显示充值按钮，如果小程序没有资质审核不过可不开启</p>
        </div>
      </el-form-item>

      <el-form-item label="是否开启自定义金额">
        <el-radio-group v-model="form.is_plan">
          <el-radio label="1">开启</el-radio>
          <el-radio label="0">关闭</el-radio>
        </el-radio-group>
        <div class="lh18 mt10 gray9">
          <p>注：如开启则用户可以填写充值金额</p>
        </div>
      </el-form-item>

      <el-form-item label="最低充值金额" prop="min_money" :rules="[{required: true,message: ' '}]">
        <el-input v-model="form.min_money" placeholder="请输入内容" class="max-w460">
          <template slot="append">元</template>
        </el-input>
        <div class="lh18 mt10 gray9">
          <p> 注：自定义充值金额最低充值多少元</p>
        </div>
      </el-form-item>

      <el-form-item label="充值说明" :rules="[{required: true,message: ' '}]">
        <el-input v-model="form.describe" type="textarea" rows="5" autocomplete="off" />
      </el-form-item>

      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" size="small" :loading="loading" @click="onSubmit">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import BalanceApi from '@/api/balance.js'

export default {
  data() {
    return {
      form: {
      },
      loading: false
    }
  },
  created() {
    /* 获取数据 */
    this.getData()
  },
  methods: {
    /** 获取数据 **/
    getData() {
      const self = this
      const Params = {}
      BalanceApi.getSettings(Params, true).then(data => {
        self.form = data.data.values
      }).catch(() => {
      })
    },
    /** 保存 **/
    onSubmit() {
      const self = this
      const form = self.form
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.loading = true
          BalanceApi.setSettings(form, true)
            .then(data => {
              self.loading = false
              if (data.code === 1) {
                self.$message({
                  message: '恭喜你，保存成功',
                  type: 'success'
                })
              } else {
                self.loading = false
              }
            })
            .catch(() => {
              self.loading = false
            })
        }
      })
    }
  }
}
</script>
